#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@163.com>
# CreateTime: 2020-12-10 09:29:27

"""
https://www.cnblogs.com/dalaoban/p/9331113.html
"""

import socket


sock = socket.socket()          # 创建套接字
# 允许地址重用
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 8898))  # 把地址绑定到套接字
sock.listen()                   # 开始监听连接

while True:
    conn, addr = sock.accept()  # 接受客户端链接
    ret = conn.recv(1024)       # 接收客户端信息
    print(ret)                  # 打印客户端信息
    resp = b'Yes, I have received'
    conn.send(resp)             # 向客户端发送信息
    conn.close()                # 关闭客户端套接字

sock.close()        # 关闭服务器套接字(可选)
